package com.project.movie.controller.favor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.MovieFavor;
import com.project.movie.domain.MovieItem;
import com.project.movie.domain.Result;
import com.project.movie.domain.User;
import com.project.movie.service.MovieFavorService;
import com.project.movie.service.MovieItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 收藏controller
 * @date 2025/02/07 08:57
 */
@Controller
@ResponseBody
@RequestMapping("favor")
public class MovieFavorController {

    @Autowired
    private MovieFavorService movieFavorService;
    @Autowired
    private MovieItemService movieItemService;

    /** 分页获取收藏 */
    @PostMapping("getMovieFavorPage")
    public Result getMovieFavorPage(@RequestBody MovieFavor movieFavor) {
        User user = ShiroUtils.getUserInfo();
        movieFavor.setUserId(user.getId());
        Page<MovieFavor> page = new Page<>(movieFavor.getPageNumber(),movieFavor.getPageSize());
        QueryWrapper<MovieFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieFavor.getItemId()),MovieFavor::getItemId,movieFavor.getItemId())
                .eq(StringUtils.isNotBlank(movieFavor.getUserId()),MovieFavor::getUserId,movieFavor.getUserId())
                .eq(StringUtils.isNotBlank(movieFavor.getCreateBy()),MovieFavor::getCreateBy,movieFavor.getCreateBy())
                .eq(movieFavor.getCreateTime() != null,MovieFavor::getCreateTime,movieFavor.getCreateTime())
                .eq(StringUtils.isNotBlank(movieFavor.getUpdateBy()),MovieFavor::getUpdateBy,movieFavor.getUpdateBy())
                .eq(movieFavor.getUpdateTime() != null,MovieFavor::getUpdateTime,movieFavor.getUpdateTime());
        Page<MovieFavor> movieFavorPage = movieFavorService.page(page, queryWrapper);
        for (MovieFavor favor : movieFavorPage.getRecords()) {
            MovieItem movieItem = movieItemService.getById(favor.getItemId());
            favor.setCover(movieItem.getCover());
            favor.setName(movieItem.getName());
            favor.setLikeNum(movieItem.getLikeNum());
            favor.setScore(movieItem.getScore());
        }
        return Result.success(movieFavorPage);
    }

    /** 根据id获取收藏 */
    @GetMapping("getMovieFavorById")
    public Result getMovieFavorById(@RequestParam("id")String id) {
        MovieFavor movieFavor = movieFavorService.getById(id);
        return Result.success(movieFavor);
    }

    /** 保存收藏 */
    @PostMapping("saveMovieFavor")
    public Result saveMovieFavor(@RequestBody MovieFavor movieFavor) {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<MovieFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieFavor::getItemId,movieFavor.getItemId())
                .eq(MovieFavor::getUserId,userInfo.getId());
        int count = movieFavorService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("您已收藏过该电影");
        }
        movieFavor.setUserId(userInfo.getId());
        boolean save = movieFavorService.save(movieFavor);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑收藏 */
    @PostMapping("editMovieFavor")
    public Result editMovieFavor(@RequestBody MovieFavor movieFavor) {
        boolean save = movieFavorService.updateById(movieFavor);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除收藏 */
    @GetMapping("removeMovieFavor")
    public Result removeMovieFavor(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                movieFavorService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("收藏id不能为空！");
        }
    }

}
